package com.zsjt.app.mapper;

import com.zsjt.app.pojo.UserDto;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Repository("userDtoMapper")
public interface UserDtoMapper {
    /**
     *检验邮箱是否存在
     * @param userEmail
     * @return
     */
    @Select(value = "select * from t_user where user_email=#{email}")
    @Results(id = "userMap",value = {
            @Result(id=true,column = "user_id",property = "id"),
            @Result(column = "user_type",property = "type"),
            @Result(column = "user_name",property = "name"),
            @Result(column = "user_account",property = "account"),
            @Result(column = "user_email",property = "email"),
            @Result(column = "user_phone",property = "phone"),
            @Result(column = "user_password",property = "password"),
    })
     UserDto isEmailExist(@Param("email") String userEmail);

    /**
     * 检验手机号码是否存在
     * @param userPhone
     * @return
     */
    @Select(value = "select * from t_user where user_phone=#{phone}")
    @ResultMap(value = "userMap")
     UserDto isPhoneExist(@Param("phone") String userPhone);

    /**
     * 通过账号（用户名，手机号码，邮箱）查询用户
     * @param user_account
     * @return
     */
    @Select(value = "select * from t_user where user_account=#{account} or user_email=#{account} or user_phone=#{account}")
    @ResultMap(value = "userMap")
     UserDto findUserByAccount(@Param("account") String user_account);

    /**
     * 根据用户id查询用户
     * @param userId
     * @return
     */
    @Select(value = "select * from t_user where user_id =#{userId}")
    @ResultMap(value = "userMap")
     UserDto findUserById(@Param("userId") Integer userId);

    /**
     * 添加用户
     * @param user
     */
    @Insert(value = "insert into " +
            "t_user(user_type,user_name,user_account,user_email,user_phone,user_password) " +
            "values(#{type},#{name},#{account},#{email},#{phone},#{password})")
     void saveUser(UserDto user);

    /**
     * 更新用户
     * @param user
     */
    @Update(value = "update t_user " +
            "set user_type=#{type},user_name=#{name},user_account=#{account},user_email=#{email},user_phone=#{phone},user_password=#{password}" +
            " where user_id =#{id}")
     void updateUser(UserDto user);

    /**
     * 删除用户
     * @param userId
     */
    @Delete(value = "delete from t_user where user_id =#{userId}")
     void delete(@Param("userId") Integer userId);
}
